package ar.edu.uba.fi.jmit.monitorengine.core;

import ar.edu.uba.fi.jmit.integrationcommons.Event;
import ar.edu.uba.fi.jmit.integrationcommons.EventPriority;
import ar.edu.uba.fi.jmit.monitorengine.Constants;
import ar.edu.uba.fi.jmit.monitorengine.MonitorEngineTestCase;
import ar.edu.uba.fi.jmit.monitorengine.ResourceProperties;
import ar.edu.uba.fi.jmit.monitorengine.mock.MonitorEngineSenderImplSpy;

public class MonitorEngineSenderImplTestCase extends MonitorEngineTestCase {

	public void testSendOneVisualizer() {
		MonitorEngineSenderImplSpy sender = new MonitorEngineSenderImplSpy();
		sender.addVisualizerFor("testMonitor", "visualizerName");
		Event event = this.createEvent("testMonitor");
		assertEquals(0, sender.getSenderMock().getAllSendedEvents().size());
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName"));
		sender.sendEvent(event);
		assertEquals(1, sender.getSenderMock().getAllSendedEvents().size());
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName").size());
		assertEquals(event, sender.getSenderMock().getSendedEvents("visualizerName").get(0));
	}
	
	public void testSendManyVisualizers() {
		MonitorEngineSenderImplSpy sender = new MonitorEngineSenderImplSpy();
		sender.addVisualizerFor("testMonitor", "visualizerName1");
		sender.addVisualizerFor("testMonitor", "visualizerName2");
		sender.addVisualizerFor("testMonitor", "visualizerName3");
		sender.addVisualizerFor("testMonitor", "visualizerName4");
		sender.addVisualizerFor("testMonitor", "visualizerName5");
		Event event = this.createEvent("testMonitor");
		assertEquals(0, sender.getSenderMock().getAllSendedEvents().size());
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName1"));
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName2"));
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName3"));
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName4"));
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName5"));
		sender.sendEvent(event);
		assertEquals(5, sender.getSenderMock().getAllSendedEvents().size());
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName1").size());
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName2").size());
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName3").size());
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName4").size());
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName5").size());
	}
	
	public void testSendDiferentMonitors() {
		MonitorEngineSenderImplSpy sender = new MonitorEngineSenderImplSpy();
		sender.addVisualizerFor("testMonitor", "visualizerName");
		sender.addVisualizerFor("testMonitor2", "visualizerName2");
		Event event = this.createEvent("testMonitor");
		assertEquals(0, sender.getSenderMock().getAllSendedEvents().size());
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName"));
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName2"));
		sender.sendEvent(event);
		assertEquals(1, sender.getSenderMock().getAllSendedEvents().size());
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName").size());
		assertEquals(event, sender.getSenderMock().getSendedEvents("visualizerName").get(0));
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName2"));
		
		sender.sendEvent(this.createEvent("testMonitor2"));
		assertEquals(1, sender.getSenderMock().getSendedEvents("visualizerName2").size());
		
		for(int i = 2; i < 10; i++) {
			sender.sendEvent(this.createEvent("testMonitor2"));
			assertEquals(i, sender.getSenderMock().getSendedEvents("visualizerName2").size());
		}
	}
	
	public void testHpSend() {
		MonitorEngineSenderImplSpy sender = new MonitorEngineSenderImplSpy();
		sender.addVisualizerFor("testMonitor", "visualizerName");
		Event event = this.createEvent("testMonitor");
		event.setPriority(EventPriority.ALERT);
		assertEquals(0, sender.getSenderMock().getAllSendedEvents().size());
		assertNull(sender.getSenderMock().getSendedEvents("visualizerName"));
		sender.sendEvent(event);
		assertEquals(1, sender.getSenderMock().getAllSendedEvents().size());
		assertEquals(1, sender.getSenderMock().getHpSendedEvents("visualizerName").size());
		assertEquals(event, sender.getSenderMock().getHpSendedEvents("visualizerName").get(0));
	}
	
	private Event createEvent(String eventName) {
		Event event = new Event();
		event.setSource(eventName);
		event.setPriority(EventPriority.NORMAL);
		event.addData(ResourceProperties.getProperty(Constants.EVENT_MONITOR_NAME_KEY), eventName);
		event.addData(ResourceProperties.getProperty(Constants.EVENT_MONITOR_DATE), "new java.util.Date()");
		return event;
	}
}
